package br.com.portalecbahia.dao;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import br.com.portalecbahia.model.Player;
import br.com.portalecbahia.util.PortalECBahiaUtil;

public class PlayerDAO {
	private static final String LOG_TAG = "PlayerDAO";
	private Context context;
	private SQLiteDatabase database;
	private DBHelper dbHelper;

	public PlayerDAO(Context context) {
		this.context = context;
	}

	public PlayerDAO openWritable() throws SQLException {
		dbHelper = new DBHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	public PlayerDAO openReadable() throws SQLException {
		dbHelper = new DBHelper(context);
		database = dbHelper.getReadableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}


	public List<Player> findAll () {
		this.openReadable();

		List<Player> result = new ArrayList<Player>();		
		Cursor cursor;
			 
		try{
			cursor = database.rawQuery("SELECT * FROM tbl_player order by idPosition, ID", null);
			cursor.moveToFirst();
			
			if (!cursor.isAfterLast()){
				do{
					result.add(this.bindSQLite(cursor));
				}while (cursor.moveToNext());
			}
			
			cursor.close();
		}catch (SQLException e){
			Log.e(LOG_TAG, e.toString());
		}
		
		this.close();
		
		return result;
	}
	
	
	public Player findByPrimaryKey (Player player) {
		this.openReadable();

		Cursor cursor;
		Player retorno = null;	 
		try{
			cursor = database.rawQuery("SELECT * FROM tbl_player WHERE ID = "+player.getId(), null);
			cursor.moveToFirst();
			
			if (!cursor.isAfterLast()){
				retorno =  this.bindSQLite(cursor);
				
			}
			
			cursor.close();
		}catch (SQLException e){
			Log.e(LOG_TAG, e.toString());
		}
		
		this.close();
		
		 return retorno;
	}

	
	private Player bindSQLite(Cursor cursor){
		Player player  = new Player();
		
		for (int i = 0; i < cursor.getColumnCount(); i++){

			if("ID".equals(cursor.getColumnName(i)))
				player.setId(cursor.getInt(i));
			else if("name".equals(cursor.getColumnName(i))) 
				player.setName(cursor.getString(i));
			else if("idPosition".equals(cursor.getColumnName(i)))
				player.setIdPosition(cursor.getInt(i));
			else if("position".equals(cursor.getColumnName(i)))
				player.setPosition(cursor.getString(i));
			else if("description".equals(cursor.getColumnName(i)))
				player.setDescription(cursor.getString(i));
			else if("urlImage".equals(cursor.getColumnName(i)))
				player.setUrlImage(cursor.getString(i));	
				
		}
		return player;
	}

	public void insert(JSONArray jsonArray) throws JSONException {
		this.openWritable();
		Cursor cursor = null;
		try{
			database.beginTransaction();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject json = jsonArray.getJSONObject(i);
				


				cursor = database.rawQuery(" INSERT INTO tbl_player (ID,  name  ,  urlImage ,  position, idPosition, description )  " +
						" VALUES ("+json.getLong("id")+", '"+json.getString("name")+"','"+json.getString("url_image")+"', '"+json.getString("position")+"', "+json.getInt("idPosition")+", '"+json.getString("description")+"');", null);
	
					cursor.moveToFirst();
					cursor.close();
				
			
			}
		}catch (SQLException e) {
			Log.e(LOG_TAG, e.toString());
		}finally {
			database.setTransactionSuccessful();
			database.endTransaction();
		}
		
		this.close();
		
	}
	
	public void removeAll() {
		this.openWritable();
		Cursor cursor = null;
		try{
			cursor = database.rawQuery("DELETE FROM tbl_player", null);
			cursor.moveToFirst();
			cursor.close();
		}catch (SQLException e) {
			Log.e(PortalECBahiaUtil.TAG, e.toString());
		}
		this.close();
		
	}

	


	
}


